package main

import (
	"fmt"
	"github.com/gorhill/cronexpr"
	"strconv"
	"strings"
	"time"
)

func main() {
	startReport()

}

// 定时上报数据
func startReport() {
	//获取配置文件信息
	reportWeek := "6,7,4"
	reportTime := strings.Split("11:21", ":")
	//构造定时任务
	//周转换
	reportWeek = parseWeek(reportWeek)
	hour, _ := strconv.Atoi(reportTime[0])
	minute, _ := strconv.Atoi(reportTime[1])

	cronTimer := fmt.Sprintf("0 %d %d * * %s * ", minute, hour, reportWeek)
	fmt.Println(cronTimer)

	cron := cronexpr.MustParse(cronTimer)
	//计算下次触发时间的时间对象
	next := cron.Next(time.Now())
	for {
		now := time.Now()
		if next.Before(now) || next.Equal(now) {
			//TODO 此处填写任务代码
			fmt.Println("测试任务执行-----------------------------")
			next = cron.Next(now)
		}
		select {
		case <-time.NewTicker(time.Second).C:
		}
	}
}

func parseWeek(week string) string {
	weeks := strings.Replace(week, "7", "0", 1)
	return weeks
}
